/*****************************************************************************
*        描述 :
*             文件描述
*        CLR版本:            3.5
*        创建者 :            蒋 光
*        个人域名:           jiangguang.net.cn
*        命名空间名称:       ECP.DataAccess
*        文件名:             BaseNoteDao.cs
*        创建系统时间:       2012/11/2 19:06:59
*        创建年份:           2012
*
/*****************************************************************************/

namespace ECP.DataAccess
{
    using System;
    using System.Data;
    using ECP.DataAccess.Utilities;
    using ECP.Model;
    using ECP.Utilities;

    public class BaseNoteDao : BaseDao
    {
        public BaseNoteDao()
        {
            base.CurrentTableName = BaseNoteTable.TableName;
        }

        public BaseNoteDao(IDbHelper dbHelper)
            : this()
        {
            base.DbHelper = dbHelper;
        }

        public BaseNoteDao(BaseUserInfo userInfo)
            : this()
        {
            base.UserInfo = userInfo;
        }

        public BaseNoteDao(IDbHelper dbHelper, BaseUserInfo userInfo)
            : this(dbHelper)
        {
            base.UserInfo = userInfo;
        }

        public string Add(BaseNoteEntity noteEntity)
        {
            return this.AddEntity(noteEntity);
        }

        public string AddEntity(BaseNoteEntity noteEntity)
        {
            string targetValue = Guid.NewGuid().ToString();
            SQLBuilder sqlBuilder = new SQLBuilder(base.DbHelper);
            sqlBuilder.BeginInsert(BaseNoteTable.TableName);
            sqlBuilder.SetValue(BaseNoteTable.FieldID, targetValue);
            this.SetEntity(sqlBuilder, noteEntity);
            sqlBuilder.SetValue(BaseNoteTable.FieldCreateUserID, base.UserInfo.ID);
            sqlBuilder.SetDBNow(BaseNoteTable.FieldCreateDate);
            sqlBuilder.SetDBNow(BaseNoteTable.FieldModifyDate);
            if (sqlBuilder.EndInsert() <= 0)
            {
                return string.Empty;
            }
            return targetValue;
        }

        public DataTable GetDTByUser(string userID, bool deleteMark)
        {
            string sqlQuery = string.Concat(new object[] { " SELECT *  FROM ", BaseNoteTable.TableName, " WHERE (", BaseNoteTable.FieldCreateUserID, " = ", base.DbHelper.GetParameter(BaseNoteTable.FieldCreateUserID), " AND ", BaseNoteTable.FieldDeleteMark, " = ", deleteMark ? 1 : 0, ") ORDER BY ", BaseNoteTable.FieldSortCode });
            string[] targetFileds = new string[1];
            object[] targetValues = new object[1];
            targetFileds[0] = BaseNoteTable.FieldCreateUserID;
            targetValues[0] = userID;
            DataTable dataTable = new DataTable(BaseNoteTable.TableName);
            base.DbHelper.Fill(dataTable, sqlQuery, base.DbHelper.MakeParameters(targetFileds, targetValues));
            return dataTable;
        }

        public DataTable GetDTByUser(string userID, bool deleteMark, string search)
        {
            if (search.Length == 0)
            {
                return this.GetDTByUser(userID, deleteMark);
            }
            search = BaseBusinessLogic.GetSearchString(search);
            DataTable dataTable = new DataTable(BaseNoteTable.TableName);
            string sqlQuery = string.Concat(new object[] {
                " SELECT *  FROM ", BaseNoteTable.TableName, " WHERE ((", BaseNoteTable.FieldTitle, " LIKE ", base.DbHelper.GetParameter(BaseNoteTable.FieldTitle), " ) OR (", BaseNoteTable.FieldContent, " LIKE ", base.DbHelper.GetParameter(BaseNoteTable.FieldContent), " ) OR (", BaseNoteTable.FieldCategoryFullName, " LIKE ", base.DbHelper.GetParameter(BaseNoteTable.FieldCategoryFullName), " ) OR (CONVERT (NVARCHAR(10), ", BaseNoteTable.FieldCreateDate,
                ", 20) LIKE ", base.DbHelper.GetParameter(BaseNoteTable.FieldCreateDate), " )) AND (", BaseNoteTable.FieldCreateUserID, " = ", base.DbHelper.GetParameter(BaseNoteTable.FieldCreateUserID), " AND ", BaseNoteTable.FieldDeleteMark, " = ", deleteMark ? 1 : 0, ") ORDER BY ", BaseNoteTable.FieldSortCode
             });
            string[] targetFileds = new string[5];
            object[] targetValues = new object[5];
            targetFileds[0] = BaseNoteTable.FieldTitle;
            targetValues[0] = search;
            targetFileds[1] = BaseNoteTable.FieldContent;
            targetValues[1] = search;
            targetFileds[2] = BaseNoteTable.FieldCategoryFullName;
            targetValues[2] = search;
            targetFileds[3] = BaseNoteTable.FieldCreateDate;
            targetValues[3] = search;
            targetFileds[4] = BaseNoteTable.FieldCreateUserID;
            targetValues[4] = userID;
            base.DbHelper.Fill(dataTable, sqlQuery, base.DbHelper.MakeParameters(targetFileds, targetValues));
            return dataTable;
        }

        private void SetEntity(SQLBuilder sqlBuilder, BaseNoteEntity noteEntity)
        {
            sqlBuilder.SetValue(BaseNoteTable.FieldTitle, noteEntity.Title);
            sqlBuilder.SetValue(BaseNoteTable.FieldCategoryID, noteEntity.CategoryID);
            sqlBuilder.SetValue(BaseNoteTable.FieldCategoryFullName, noteEntity.CategoryFullName);
            sqlBuilder.SetValue(BaseNoteTable.FieldColor, noteEntity.Color);
            sqlBuilder.SetValue(BaseNoteTable.FieldContent, noteEntity.Content);
            sqlBuilder.SetValue(BaseNoteTable.FieldEnabled, noteEntity.Enabled);
            sqlBuilder.SetValue(BaseNoteTable.FieldImportant, noteEntity.Important);
        }

        public int Update(BaseNoteEntity noteEntity)
        {
            return this.UpdateEntity(noteEntity);
        }

        public int UpdateEntity(BaseNoteEntity noteEntity)
        {
            SQLBuilder sqlBuilder = new SQLBuilder(base.DbHelper);
            sqlBuilder.BeginUpdate(BaseNoteTable.TableName);
            this.SetEntity(sqlBuilder, noteEntity);
            sqlBuilder.SetValue(BaseNoteTable.FieldModifyUserID, base.UserInfo.ID);
            sqlBuilder.SetDBNow(BaseNoteTable.FieldModifyDate);
            sqlBuilder.SetWhere(BaseNoteTable.FieldID, noteEntity.ID);
            return sqlBuilder.EndUpdate();
        }
    }
}